package net.poemcode.gzlib.detail;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import net.poemcode.gzlib.Cache;
import net.poemcode.gzlib.LibraryOpenHelper;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.ContentValues;
import android.content.Context;
import android.util.Xml;

public class DetailPullParser {
	
	public static List<DetailResult> getDetailList(final Context context, final InputStream inputStream) throws XmlPullParserException, IOException {
		List<DetailResult> detailList = new LinkedList<DetailResult>();
		
		XmlPullParser parser = Xml.newPullParser();  
        parser.setInput(inputStream, "UTF-8");  
        int event = parser.getEventType();
        while(event!=XmlPullParser.END_DOCUMENT){  
            switch(event){  
            case XmlPullParser.START_TAG:
            	String tagName = parser.getName();
            	if ("ROWSET1".equals(tagName)) {
            		parseDetailResult(parser, detailList);
            		boolean isCacheValid = Cache.getInstance(context).isValid();  
            		if (isCacheValid) {
                		return detailList;
                	}
            	} 
            	
            	if ("ROWSET3".equals(tagName)) {
            		// 解析lib
            		parseLib(context, parser);
            	} else if ("ROWSET4".equals(tagName)) {
            		// 解析local
            		parseLocal(context, parser);
            	} else if ("ROWSET".equals(tagName)) {
            		// 解析state
            		parseState(context, parser);
            	} else if ("ROWSET5".equals(tagName)) {
            		// 解析cir
            		parseCir(context, parser);
            	}
            	break;
            }
            event = parser.next();
        }
        
		return detailList;
	}
	
	private static void parseDetailResult(XmlPullParser parser, List<DetailResult> list) throws XmlPullParserException, IOException {
		DetailResult detail = null;
		int event = parser.getEventType();
		while(event!=XmlPullParser.END_DOCUMENT){  
            switch(event){  
            case XmlPullParser.START_TAG:
            	String qName = parser.getName();
            	if (qName.equals("ROW")) {
            		detail = new DetailResult();
        			list.add(detail);
        		} else if (qName.equals("BARCODE")) {
        			detail.setBarcode(parser.nextText());
        		} else if (qName.equals("STATE")) {
        			detail.setState(parser.nextText());
        		} else if (qName.equals("CALLNO")) {
        			detail.setCallNo(parser.nextText());
        		} else if (qName.equals("CURLIB")) {
        			detail.setCurlLib(parser.nextText());
        		} else if (qName.equals("CURLOCAL")) {
        			detail.setCurLocal(parser.nextText());
        		} else if (qName.equals("CIRTYPE")) {
        			detail.setCirType(parser.nextText());
        		} else if (qName.equals("VOLINFO")) {
        			detail.setVolInfo(parser.nextText());
        		}
            	break;
            case XmlPullParser.END_TAG:
            	if ("ROWSET1".equals(parser.getName())) {
            		return;
            	}
            	break;
            }
            event = parser.next();
		}
	}
	
	private static void parseLib(Context context, XmlPullParser parser) throws XmlPullParserException, IOException {
		List<ContentValues> list = new LinkedList<ContentValues>();
		ContentValues value = null;
		int event = parser.getEventType();
		while(event!=XmlPullParser.END_DOCUMENT){  
            switch(event){  
            case XmlPullParser.START_TAG:
            	String qName = parser.getName();
            	if (qName.equals("ROW")) {
            		value = new ContentValues();
        			list.add(value);
        		} else if (qName.equals("LIBCODE")) {
        			value.put(LibraryOpenHelper.COLUMN_CODE, parser.nextText());
        		} else if (qName.equals("SIMPLENAME")) {
        			value.put(LibraryOpenHelper.COLUMN_NAME, parser.nextText());
        		} 
            	break;
            case XmlPullParser.END_TAG:
            	if ("ROWSET3".equals(parser.getName())) {
            		context.getContentResolver().bulkInsert(LibraryOpenHelper.CONTENT_URI_LIB, list.toArray(new ContentValues[list.size()]));
            		Cache.getInstance(context).updateLib(context);
            		return;
            	}
            	break;
            }
            event = parser.next();
		}
	}

	private static void parseLocal(Context context, XmlPullParser parser) throws XmlPullParserException, IOException {
		List<ContentValues> list = new LinkedList<ContentValues>();
		ContentValues value = null;
		int event = parser.getEventType();
		while(event!=XmlPullParser.END_DOCUMENT){  
            switch(event){  
            case XmlPullParser.START_TAG:
            	String qName = parser.getName();
            	if (qName.equals("ROW")) {
            		value = new ContentValues();
        			list.add(value);
        		} else if (qName.equals("LOCALCODE")) {
        			value.put(LibraryOpenHelper.COLUMN_CODE, parser.nextText());
        		} else if (qName.equals("NAME")) {
        			value.put(LibraryOpenHelper.COLUMN_NAME, parser.nextText());
        		} 
            	break;
            case XmlPullParser.END_TAG:
            	if ("ROWSET4".equals(parser.getName())) {
            		context.getContentResolver().bulkInsert(LibraryOpenHelper.CONTENT_URI_LOCAL, list.toArray(new ContentValues[list.size()]));
            		Cache.getInstance(context).updateLocal(context);
            		return;
            	}
            	break;	
            }
            event = parser.next();
		}
	}
	
	private static void parseCir(Context context, XmlPullParser parser) throws XmlPullParserException, IOException {
		List<ContentValues> list = new LinkedList<ContentValues>();
		ContentValues value = null;
		int event = parser.getEventType();
		while(event!=XmlPullParser.END_DOCUMENT){  
            switch(event){  
            case XmlPullParser.START_TAG:
            	String qName = parser.getName();
            	if (qName.equals("ROW")) {
            		value = new ContentValues();
        			list.add(value);
        		} else if (qName.equals("CIRTYPE")) {
        			value.put(LibraryOpenHelper.COLUMN_CODE, parser.nextText());
        		} else if (qName.equals("NAME")) {
        			value.put(LibraryOpenHelper.COLUMN_NAME, parser.nextText());
        		} 
            	break;
            case XmlPullParser.END_TAG:
            	if ("ROWSET5".equals(parser.getName())) {
            		context.getContentResolver().bulkInsert(LibraryOpenHelper.CONTENT_URI_CIR, list.toArray(new ContentValues[list.size()]));
            		Cache.getInstance(context).updateCir(context);
            		return;
            	}
            	break;	
            }
            event = parser.next();
		}
	}
	
	private static void parseState(Context context, XmlPullParser parser) throws XmlPullParserException, IOException {
		List<ContentValues> list = new LinkedList<ContentValues>();
		ContentValues value = null;
		int event = parser.getEventType();
		while(event!=XmlPullParser.END_DOCUMENT){  
            switch(event){  
            case XmlPullParser.START_TAG:
            	String qName = parser.getName();
            	if (qName.equals("ROW")) {
            		value = new ContentValues();
        			list.add(value);
        		} else if (qName.equals("STATETYPE")) {
        			value.put(LibraryOpenHelper.COLUMN_CODE, parser.nextText());
        		} else if (qName.equals("STATENAME")) {
        			value.put(LibraryOpenHelper.COLUMN_NAME, parser.nextText());
        		} 
            	break;
            case XmlPullParser.END_TAG:
            	if ("ROWSET".equals(parser.getName())) {
            		context.getContentResolver().bulkInsert(LibraryOpenHelper.CONTENT_URI_STATE, list.toArray(new ContentValues[list.size()]));
            		Cache.getInstance(context).updateState(context);
            		return;
            	}
            	break;	
            }
            event = parser.next();
		}
	}
}
